use "party_election_data.dta" 
***separate capacity & authoritarianism from authority***
reg authority change if divi_polity == 1, r 
predict capacity, xb
gen authoritarianism = authority - capacity
reg authority change if divi_polity == 1 & v2x_regime>1, r 
predict capacity_vem, xb
gen autho_vem = authority - capacity_vem

***table1***
reghdfe elected authority if divi_polity==1, absorb(id pcode year) vce(cluster id)
est store reg1
reghdfe elected authoritarianism if divi_polity==1, absorb(id pcode year) vce(cluster id)
est store reg2
reghdfe elected capacity if divi_polity==1, absorb(id pcode year) vce(cluster id)
est store reg3
reghdfe elected authority if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store reg4
reghdfe elected authoritarianism if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store reg5
reghdfe elected capacity if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store reg6
reghdfe elected authority if divi_polity==1 & v2x_regime>1, absorb(id pcode year) vce(cluster id)
est store reg7
reghdfe elected autho_vem if divi_polity==1 & v2x_regime>1, absorb(id pcode year) vce(cluster id)
est store reg8
reghdfe elected capacity_vem if divi_polity==1 & v2x_regime>1, absorb(id pcode year) vce(cluster id)
est store reg9
esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 using maineffect.rtf, stats(N F r2 ar2) se star(* 0.1 ** 0.05 *** 0.01) replace nogap

***table2***
reghdfe elected authoritarianism polity2 c.authoritarianism#c.polity2 if divi_polity==1, absorb(id pcode year) vce(cluster id)
est store inter1
reghdfe elected capacity polity2 c.capacity#c.polity2 if divi_polity==1, absorb(id pcode year) vce(cluster id)
est store inter2
reghdfe elected authoritarianism polity2 c.authoritarianism#c.polity2 if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store inter3
reghdfe elected capacity polity2 c.capacity#c.polity2 if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store inter4
reghdfe elected autho_vem polity2 c.authoritarianism#c.polity2 if divi_polity==1 & v2x_regime>1, absorb(id pcode year) vce(cluster id)
est store inter5
reghdfe elected capacity_vem polity2 c.capacity#c.polity2 if divi_polity==1 & v2x_regime>1, absorb(id pcode year) vce(cluster id)
est store inter6
esttab inter1 inter2 inter3 inter4 inter5 inter6 using moderation.rtf, stats(N F r2 ar2) se star(* 0.1 ** 0.05 *** 0.01) replace nogap

***Figure 2: time trend analysis***
tempfile results
postfile handle year coef ci_lower ci_upper using `results', replace
forvalues y = 1990/2016 {
    di "dealing year: `y'"

    quietly reghdfe elected authoritarianism c.authoritarianism#c.year ///
        if polity2>5 & (polity2==8 | polity2==9 | polity2==10) & year>=`y', ///
        absorb(id pcode) vce(cluster id)

    local b = _b[c.authoritarianism#c.year]
    local se = _se[c.authoritarianism#c.year]
    local lb = `b' - 1.96*`se'
    local ub = `b' + 1.96*`se'

    post handle (`y') (`b') (`lb') (`ub')
}
postclose handle
use `results', clear
save "1990_results.dta", replace
use "1990_results.dta" 
twoway ///
    (rcap ci_upper ci_lower year if year < 2016, lcolor(blue%40) yaxis(1)) ///
	(rcap ci_upper ci_lower year if year == 2016, lcolor(blue%40) yaxis(2)) ///
    (scatter coef year if year < 2016, mcolor(blue) msymbol(O) msize(small) yaxis(1)) ///
    (scatter coef year if year == 2016, mcolor(blue) msymbol(O) msize(small) yaxis(2)) ///
    , ///
    ytitle("Coefficient (Left axis)", axis(1)) ///
    ytitle("2016 Coefficient (Right axis)", axis(2)) ///
    xtitle("Start year (year >= ...)") ///
    title("Temporal pattern of authoritarianism × year interaction") ///
    legend(order(2 "Coef (<2016)" 3 "Coef (2016)") pos(6))

***table3***
reghdfe authoritarianism economicgoals if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism1
reghdfe authoritarianism nationlifestyle_pos if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism2
reghdfe authoritarianism nationlifestyle_neg if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism3
reghdfe authoritarianism traditionalmorality_pos if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism4
reghdfe authoritarianism traditionalmorality_neg if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism5
reghdfe authoritarianism economicgoals nationlifestyle_pos nationlifestyle_neg traditionalmorality_pos traditionalmorality_neg if divi_polity==1 & polity2>5, absorb(id pcode year) vce(cluster id)
est store mechanism6
esttab mechanism1 mechanism2 mechanism3 mechanism4 mechanism5 mechanism6 using table3.rtf, stats(N F r2 ar2) se star(* 0.1 ** 0.05 *** 0.01) replace nogap